package com.frxuexi.apps

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.apps.entity.Apps
import com.frxuexi.apps.entity.NetAppValues
import com.google.android.material.progressindicator.CircularProgressIndicator

@Composable
fun NetAppPage(data:List<NetAppValues>,onClick: () -> Unit,downloadApk:(Apps)->Unit) {
    Column {
        Row {
            TextButton(onClick = { onClick() }) {
                Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "")
            }
            Text(text = "在线应用", modifier = Modifier.fillMaxWidth(),textAlign= TextAlign.Center)
        }
        LazyColumn(modifier = Modifier.fillMaxHeight()){
            items(data){
                AppItem(it,downloadApk)
            }
        }
    }
}

@Composable
fun AppItem(entity:NetAppValues,downloadApk:(Apps)->Unit){
    Column(verticalArrangement=Arrangement.spacedBy(10.dp)) {
        Text(text = entity.title)
        entity.appsList.forEachIndexed { index, apps ->
            if(entity.appsList.size==1){
                Row(modifier = Modifier.fillMaxWidth(),horizontalArrangement = Arrangement.SpaceAround) {
                    GridItem(apps = apps,modifier = Modifier,downloadApk)
                    GridItem(apps = apps,modifier = Modifier.alpha(0f),downloadApk)
                    GridItem(apps = apps,modifier = Modifier.alpha(0f),downloadApk)
                }

            }else{//加载3列
                if(index%3==0){
                    if(index<entity.appsList.size-1){
                        Row(modifier = Modifier.fillMaxWidth(),horizontalArrangement = Arrangement.SpaceAround) {
                            GridItem(apps = entity.appsList[index],modifier = Modifier,downloadApk)
                            GridItem(apps = entity.appsList[index+1],modifier = Modifier,downloadApk)
                            GridItem(apps = entity.appsList[index+2],modifier = Modifier,downloadApk)
                        }
                    }else{
                        Row(modifier = Modifier.fillMaxWidth(),horizontalArrangement = Arrangement.SpaceAround) {
                            GridItem(apps = apps,modifier = Modifier,downloadApk)
                            GridItem(apps = apps,modifier = Modifier.alpha(0f),downloadApk)
                            GridItem(apps = apps,modifier = Modifier.alpha(0f),downloadApk)
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun GridItem(apps: Apps,modifier: Modifier,downloadApk:(Apps)->Unit){
    var isShow by remember {
        mutableStateOf(false)
    }
    Column (modifier = Modifier.clickable {
        isShow=true

    }){
        AsyncImage(model = apps.appIcon, contentDescription = "", modifier = modifier.size(40.dp))
        Text(modifier=modifier,text = apps.appName)
    }

    if(isShow){
        AlertDialog(title={ Text(text = "下载")},
            text = { Text(text = "确认下载？")},
            onDismissRequest = {
                isShow=false
                 },
            dismissButton = {
                TextButton(
                    onClick = {
                        isShow=false
                    }
                ) {
                    Text("取消")
                }
            },
            confirmButton = {
                TextButton(
                    onClick = {
                        downloadApk(apps)
                        isShow=false
                    }
                ) {
                    Text("确认")
                }
            })
    }
}





